package com.rybbaby.tss.api.course;

import com.rybbaby.tss.core.bean.PageBean;
import com.rybbaby.tss.core.hqlsearch.HqlGenerateUtil;
import com.rybbaby.tss.core.utils.AppResponse;
import com.rybbaby.tss.core.web.controller.BaseController;
import com.rybbaby.tss.front.tssAppCourse.entity.TssAppCourse;
import com.rybbaby.tss.front.tssAppCourse.service.ITssAppCourseService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Map;

@RestController
@RequestMapping(value = "api/course", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public class CourseApi extends BaseController{

    @Autowired
    private ITssAppCourseService tssAppCourseService;

    /**
     * 获取app课程列表
     *
     * @return
     * @throws
     */
    @CrossOrigin
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public String tssAppCourseList(TssAppCourse entity, PageBean paramPageBean) {
        String resMsg = "查询成功!";
        int resCode = 200;
        PageBean pageBean=null;
        try {
            LocalDate localDate=LocalDate.now();
            LocalDate localDate1 = localDate.minusDays(7);

            Map<String, String[]> unLockMap = getParameterMap(request);
            DetachedCriteria dc = DetachedCriteria.forClass(TssAppCourse.class);
            dc.add(Restrictions.sqlRestriction("updateTime > ('"+localDate1+"')"));
            HqlGenerateUtil.fillDetachedCriteria(dc, entity, unLockMap);
            pageBean = tssAppCourseService.pageList(dc, paramPageBean.getPageNo(), paramPageBean.getPageSize());

        } catch (Exception e) {
            resCode = 500;
            resMsg = "查询失败!";
            e.printStackTrace();
        }
        return new AppResponse(resMsg, resCode,pageBean).jsonResponse(true);
    }

    @CrossOrigin
    @RequestMapping(value = "detail", method = RequestMethod.POST)
    public String toEditView(TssAppCourse entity) {
        String resMsg="查询成功";
        int resCode=200;
        TssAppCourse editEntity=null;
        if (entity != null && entity.getId() != null) {
            editEntity = this.tssAppCourseService.get(entity.getId());
        }
        return new AppResponse(resMsg, resCode,editEntity).jsonResponse(true);
    }

}
